{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import k3d\n",
    "import vtk\n",
    "import numpy as np\n",
    "from k3d.helpers import download\n",
    "import ipywidgets as widgets\n",
    "\n",
    "filename = download('https://github.com/To-Fujita/Babylon.js_3D_Graphics/raw/master/scenes/stl/Cute%20Darth%20Vader.stl')\n",
    "\n",
    "reader = vtk.vtkSTLReader()\n",
    "reader.SetFileName(filename)\n",
    "reader.Update()\n",
    "\n",
    "plot = k3d.plot()\n",
    "plot += k3d.vtk_poly_data(reader.GetOutput(), color=0x222222)\n",
    "plot.display()\n",
    "\n",
    "debug_info = widgets.HTML()\n",
    "display(debug_info)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plot.camera = [95.27, 101.43, 224.85,\n",
    " 53.44, 93.47, 46.74,\n",
    " 0.0, 1.0, 0.0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plot.camera_rotate_speed = 0.1\n",
    "plot.camera_zoom_speed = 5.0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plot.mode = 'callback'\n",
    "obj = plot.objects[0]\n",
    "obj.vertices.shape, obj.indices.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import json \n",
    "\n",
    "g = None\n",
    "def foo(params):\n",
    "    global g, debug_info\n",
    "    \n",
    "    g = params\n",
    "    debug_info.value = '<pre>' + json.dumps(params, indent=4) + \"</pre>\"\n",
    "\n",
    "obj.click_callback = foo\n",
    "obj.hover_callback = foo"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# please click or hover on Darth Vader"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "obj.click_callback = None\n",
    "obj.hover_callback = None\n",
    "\n",
    "while plot.objects:\n",
    "    plot -= plot.objects[-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def generate(dim):\n",
    "    data = np.zeros((dim, dim, dim), dtype=np.uint8)\n",
    "\n",
    "    x = np.linspace(-0.5, 0.5, dim, dtype=np.float32)\n",
    "    y = np.linspace(-0.5, 0.5, dim, dtype=np.float32)\n",
    "    z = np.linspace(-0.5, 0.5, dim, dtype=np.float32)\n",
    "\n",
    "    x, y, z = np.meshgrid(x, y, z)\n",
    "\n",
    "    c, s = np.cos(1.5 * x), np.sin(1.5 * x)\n",
    "\n",
    "    my = y * c - z * s \n",
    "    mz = y * s + z * c\n",
    "\n",
    "    my = np.fmod(my + 0.5, 0.333) * 3 - 0.5\n",
    "    mz = np.fmod(mz + 0.5, 0.333) * 3 - 0.5\n",
    "\n",
    "    displace = np.sin(60.0 * x) * np.sin(60.0 * my) * np.sin(60.0 * mz) * 0.1\n",
    "\n",
    "    data = np.sqrt(my**2 + mz**2) * (2.5 + 0.8 * np.sin(x * 50)) + displace\n",
    "    \n",
    "    return data.astype(np.float32)\n",
    "\n",
    "scalar_field = generate(256)\n",
    "\n",
    "obj = k3d.marching_cubes(scalar_field, level=0.5, color=0xaa0000)\n",
    "plot += obj"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "obj.click_callback = foo\n",
    "obj.hover_callback = foo"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# please click or hover on mesh"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "obj.click_callback = None\n",
    "obj.hover_callback = None\n",
    "\n",
    "while plot.objects:\n",
    "    plot -= plot.objects[-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "x, y = np.meshgrid(np.linspace(-3, 3, 60),np.linspace(-3, 3, 60))\n",
    "heights = np.sin(x**2 + y**2)\n",
    "obj = k3d.surface(heights.astype(np.float32), bounds=[-3,3,-3,3])\n",
    "plot += obj"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "obj.click_callback = foo\n",
    "obj.hover_callback = foo"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# please click or hover on surface"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "obj.click_callback = None\n",
    "obj.hover_callback = None\n",
    "\n",
    "while plot.objects:\n",
    "    plot -= plot.objects[-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "obj = k3d.texture(open('assets/texture.png', 'br').read(), 'png')\n",
    "plot += obj"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "obj.click_callback = foo\n",
    "obj.hover_callback = foo"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# please click or hover on texture"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "obj.click_callback = None\n",
    "obj.hover_callback = None\n",
    "\n",
    "while plot.objects:\n",
    "    plot -= plot.objects[-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "x, y = np.meshgrid(np.linspace(-3, 3, 60),np.linspace(-3, 3, 60))\n",
    "data = (np.sin(x*2.0)**2 + np.cos(y*np.sin(x))**2)\n",
    "obj = k3d.texture(attribute=data)\n",
    "plot += obj"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "obj.click_callback = foo\n",
    "obj.hover_callback = foo"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# please click or hover on texture"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.12"
  },
  "nbTranslate": {
   "displayLangs": [
    "en",
    "pl"
   ],
   "hotkey": "alt-t",
   "langInMainMenu": true,
   "sourceLang": "pl",
   "targetLang": "en",
   "useGoogleTranslate": true
  },
  "widgets": {
   "application/vnd.jupyter.widget-state+json": {
    "state": {},
    "version_major": 2,
    "version_minor": 0
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
